#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
int d[10005];
vector<int> v[10005];
int cur, ans[10005];
void topusort()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (d[i] == 0)
            q.push(i);
    while (!q.empty())
    {
        int f = q.front();
        ans[++cur] = f;
        q.pop();
        for (int i = 0; i < v[f].size(); i++)
        {
            int t = v[f][i];
            d[t]--;
            if (d[t] == 0)
                q.push(t);
        }
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        d[y]++;
        v[x].push_back(y);
    }
    topusort();
    if (cur != n) 
        printf("not sort\n");
    else
    {
        printf("sort:");
        for (int i = 1; i <= cur; i++)
            printf("%d ", ans[i]);
        puts("");
    }
    return 0;
}